<template>
  <div>
    <div style="display: flex;justify-content: flex-end;margin: 5px 0">
      <el-button size="small" type="primary" style="margin-right: 70px" @click="addSonComponent(0)">添加一级组件</el-button>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="name"
        label="组件名称"
        min-width="200">
      </el-table-column>
      <el-table-column
        prop="path"
        label="请求路劲"
        min-width="200">
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        min-width="200">
      </el-table-column>

      <el-table-column label="操作" min-width="200" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="primary"
            @click="addSonComponent(scope.row.id)">添加子组件
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index,scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="组件编辑" :visible.sync="dialog" width="600px" @close="closeDialog">
      <el-form :model="componentForm" ref="componentForm">
        <el-form-item v-show="false" label="id" label-width="80px">
          <el-input v-model="componentForm.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件名称" label-width="80px" prop="name"
                      :rules="[{required:true,message:'请输入组件名称',trigger:'blur'}]">
          <el-input v-model.trim="componentForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请求路径" label-width="80px" prop="path"
                      :rules="[{required:true,message:'请输入请求路径',trigger:'blur'}]">
          <el-input v-model="componentForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相对路径" label-width="80px" prop="component"
                      :rules="[{required:true,message:'请输入相对路径',trigger:'blur'}]">
          <el-input v-model="componentForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="80px">
          <el-input v-model.number="componentForm.remark" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新增菜单" :visible.sync="dialogAdd" width="600px" @close="closeDialogAdd">
      <el-form :model="AddForm" ref="AddForm">

        <el-form-item label="组件名称" label-width="80px" prop="name"
                      :rules="[{required:true,message:'请输入组件名称',trigger:'blur'}]">
          <el-input v-model.trim="AddForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请求路径" label-width="80px" prop="path"
                      :rules="[{required:true,message:'请输入请求路径',trigger:'blur'}]">
          <el-input v-model="AddForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相对路径" label-width="80px" prop="component"
                      :rules="[{required:true,message:'请输入请求路径',trigger:'blur'}]">
          <el-input v-model="AddForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="80px">
          <el-input v-model.number="AddForm.remark" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="closeDialogAdd">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "ComponentManager",
  data() {
    return {
      tableData: [],
      dialog: false,
      componentForm: {
        id: '',
        name: '',
        path: '',
        component: '',
        parentId: '',
        remark: ''
      },
      dialogAdd: false,
      AddForm: {
        name: '',
        path: '',
        component: '',
        parentId: '',
        remark: ''
      }
    }
  }, methods: {
    addSonComponent(parentId) {
      this.dialogAdd = true;
      this.AddForm.parentId = parentId;
    },
    submitAddForm() {
      this.$refs['AddForm'].validate(valid => {
        if (valid) {
          this.$axios.post('/component/addComponent', this.$qs.stringify(this.AddForm)).then(res => {
            if (res.code === 200) {
              this.$message({type: 'success', message: '添加成功!'});
              this.closeDialogAdd();
              this.getData();
            }
          });
        }
      });
    },
    handleEdit(row) {
      this.componentForm.id = row.id;
      this.componentForm.name = row.name;
      this.componentForm.path = row.path;
      this.componentForm.component = row.component;
      this.componentForm.remark = row.remark;
      this.componentForm.parentId = row.parentId;
      this.dialog = true;
    },
    handleDelete(index, row) {
      this.$confirm('确认删除?').then(() => {
        this.$axios.post('/component/deleteComponent', this.$qs.stringify({id: row.id})).then(res => {
          if (res.code === 200) {
            this.$message({type: 'success', duration: 1000, message: '删除成功!'});
            this.getData();
          }
        });
      }).catch(() => {
      });
    },
    submit() {
      this.$refs['componentForm'].validate(valid => {
        if (valid) {
          this.$axios.post('/component/editComponent', this.$qs.stringify(this.componentForm)).then(res => {
            if (res.code === 200) {
              this.$message({type: 'success', duration: 1000, message: '修改成功!'});
              this.getData();
              this.closeDialog();
            }
          })
        }
      });
    },
    closeDialog() {
      this.dialog = false;
      this.componentForm = {
        id: '',
        name: '',
        path: '',
        component: '',
        parentId: '',
        remark: ''
      };
      this.$refs['componentForm'].resetFields();
    },
    closeDialogAdd() {
      this.dialogAdd = false;
      Object.keys(this.AddForm).forEach(key => {
        this.AddForm[key] = '';
      });
      this.$refs['AddForm'].resetFields();//清除验证
    },
    getData() {
      this.$axios.post('/component/findAll').then(res => {
        if (res.code === 200) {
          this.tableData = res.data.components;
        }
      });
    }
  }, created() {
    this.getData();
  }
}
</script>

<style scoped>

</style>
